package com.xtkj.accountjava.service.impl;

import com.xtkj.accountjava.base.Result;
import com.xtkj.accountjava.entity.AccountBookEntity;
import com.xtkj.accountjava.entity.LogEntity;
import com.xtkj.accountjava.entity.UserEntity;
import com.xtkj.accountjava.mapper.UserMapper;
import com.xtkj.accountjava.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public List<UserEntity> getAllUser() {
        return userMapper.getAllUser();
    }

    @Override
    public UserEntity checkToken(String token) {
        if(token!=null&&(token.contains("Bearer "))){
            token=token.replaceFirst("Bearer ","");
        }
        return userMapper.checkToken(token);
    }

    @Override
    public UserEntity findUserById(int id) {
        return userMapper.findUserById(id);
    }



    @Override
    public UserEntity findUserByOpenid(String openid) {
        return userMapper.findUserByOpenid(openid);
    }


    @Override
    public UserEntity addUser(UserEntity userEntity) {
        Integer insert = userMapper.insert(userEntity);
        if(insert==0){
            return  null;
        }else {
            return  userEntity;
        }
    }
    @Override
    public UserEntity modifyUser(UserEntity userEntity) {
        Integer updateById = userMapper.updateById(userEntity);
        if(updateById==0){
            return  null;
        }
        return userEntity;
    }

    @Override
    public UserEntity findUserByUserAccount(String account) {

        return userMapper.findUserByUserAccount(account);
    }

    @Override
    public UserEntity innerEditUser(Map<String, Object> parmsMap) {
        int userId = new BigDecimal(parmsMap.get("userId")+"").intValue();
        UserEntity userEntity = findUserById(userId);

        //修改默认账本
        if(parmsMap.get("name")!=null)userEntity.setName((String) parmsMap.get("name"));
        if(parmsMap.get("password")!=null)userEntity.setPassword((String) parmsMap.get("password"));
        if(parmsMap.get("avatar")!=null)userEntity.setAvatar((String) parmsMap.get("avatar"));
        if(parmsMap.get("dayBeanNum")!=null)userEntity.setDayBeanNum((int) parmsMap.get("dayBeanNum"));
        if(parmsMap.get("sumBeanNum")!=null)userEntity.setSumBeanNum((int) parmsMap.get("sumBeanNum"));
        if(parmsMap.get("modifyTime")!=null)userEntity.setModifyTime(new Date());
        if(parmsMap.get("sex")!=null)userEntity.setSex((int) parmsMap.get("sex"));
        if(parmsMap.get("province")!=null)userEntity.setProvince((String) parmsMap.get("province"));
        if(parmsMap.get("city")!=null)userEntity.setCity((String) parmsMap.get("city"));
        if(parmsMap.get("country")!=null)userEntity.setCountry((String) parmsMap.get("country"));
        if(parmsMap.get("defAcbookId")!=null)userEntity.setDefAcbookId((int) parmsMap.get("defAcbookId"));
        if(parmsMap.get("defAcbookName")!=null)userEntity.setDefAcbookName((String) parmsMap.get("defAcbookName"));

        UserEntity modifyUser = modifyUser(userEntity);
        return  modifyUser;
    }


}
